<template>
	<gui-page ref="guipage" :apiLoadingStatus="apiLoadingStatus" :refresh="true" @reload="reload" :loadmore="true"
		@loadmorefun="getData">
		<view slot="gBody">
			<gui-back></gui-back>
			<view class="title-view titlePosition">
				<!-- 居中标题 -->
				<view class="gui-flex gui-rows gui-nowrap gui-justify-content-center gui-align-items-center">
					<text class="gui-color-gray gui-icons small-text">&#xe69b;</text>
					<text class="gui-primary-color gui-h6 title1-margin gui-title-text">{{curriculumName}}</text>
					<text class="gui-color-gray gui-icons small-text">&#xe69b;</text>
				</view>
			</view>
			<view class="titlePosition-place"></view>
			<view class="data-module">
				<gui-empty v-if="noData">
					<view slot="img" class="gui-flex gui-rows gui-justify-content-center">
						<!-- 请根据您的项目要求制作并更换为空图片 -->
						<image class="gui-empty-img" src="/static/noImg.png" mode="widthFix"></image>
					</view>
					<text slot="text" class="gui-text gui-block-text gui-text-center gui-margin-top"
						style="color:#666;padding: 0 100rpx;">暂无数据！</text>
				</gui-empty>
				<view class="data-list">
					<view class="data-item bgfff" v-for="(item,index) in dataList" :key="item.id">
						<cpdd-swipe-action>
							<cpdd-swipe-action-item :index="index" @click="handleClick($event, index)"
								:rightOptions="rightOptions">
								<view v-for="(ele,idx) in columns" :key="ele.id">
									<view class="item-cell flex-ac" v-if="idx<5">
										<text class="data-item-label">{{ele.label}}：</text>
										<text class="data-item-val flex1 qoverDot">{{item[ele.prop]}}</text>
									</view>
								</view>
								<cpdd-collapse v-if="Object.keys(columns).length>5">
									<cpdd-collapse-item>
										<view v-for="(ele,idx) in columns" :key="ele.id">
											<view class="item-cell flex-ac" v-if="idx>=5">
												<text class="data-item-label">{{ele.label}}：</text>
												<text class="data-item-val flex1 qoverDot">{{item[ele.prop]}}</text>
											</view>
										</view>
									</cpdd-collapse-item>
								</cpdd-collapse>
							</cpdd-swipe-action-item>
						</cpdd-swipe-action>
					</view>
				</view>
			</view>
			<view class="com-addBtn flex-ac-jc" @click="goOptions('add')">
				<text class="gui-icons f40 cfff">&#xe6c7;</text>
			</view>
		</view>
	</gui-page>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import {
		columns
	} from './data.js'
	export default {
		data() {
			return {
				query: { //公用查询
					curriculumId: '',
					pageNum: 1,
					pageSize: 10
				},
				curriculumName:'',
				totalPage: 1, //总页数
				apiLoadingStatus: false, //加载
				dataList: [], //数据
				noData: false, //是否无数据
				rightOptions: [{
						text: '查看',
						style: {
							backgroundColor: '#19be6b'
						}
					},
					{
						text: '编辑',
						style: {
							backgroundColor: '#2979ff',
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}, {
						text: '题目\n管理',
						style: {
							backgroundColor: '#48D1CC',
						}
					},
				]
			}
		},
		computed: {
			columns() {
				return columns
			}
		},
		onLoad(options) {
			this.query.curriculumId = options.id ? options.id : ''
			this.curriculumName = options.name ? options.name : ''
			this.getData()
			uni.$on('reFlashList', () => {
				this.reload(false)
			})
		},
		onUnload() {
			uni.$off('reFlashList')
		},
		methods: {
			handleClick(e, index) { //左滑操作列表点击
				const item = this.dataList[index]
				if (!item || !item.id) return this.$u.msg('未获取到id')
				if (e.content.text == '查看') {
					this.goOptions('look', item.id)
				} else if (e.content.text == '编辑') {
					this.goOptions('edit', item.id)
				} else if (e.content.text == '删除') {
					this.handleDel(item.id)
				}  else if (e.content.text == '题目\n管理') {
                   this.$u.to('/pages/aqpx/kcgl-warp/topic/index?id='+item.id+'&name='+item.name+'&curriculumId='+this.query.curriculumId+'&curriculumName='+this.curriculumName)
				}
			},
			goOptions(op, id) { //操作跳转详情
				let idParams = op != 'add' ? `&id=${id}` : '' //判断要不要带id过去
				this.$u.to(`./form?op=${op}` + idParams+'&curriculumId='+this.query.curriculumId)
			},
			handleDel(id) { //删除
				this.$http.delete(`/training/chapter/${id}`).then(res => {
					let result = res.data
					console.log('', result)
					this.$u.msg('删除成功！')
					this.reload(false);
				})
			},
			getData: function(isReload) { //获取列表数据
				this.apiLoadingStatus = true;
				console.log('加载函数运行，页码 : ' + this.query.pageNum);
				this.$http.get('/training/chapter/list', {
					...this.query,
					...this.searchForm
				}).then(res => {
					let result = res.rows
					console.log('列表', result)
					this.totalPage = Math.ceil(res.total / this.query.pageSize);
					if (this.query.pageNum >= 2) {
						this.dataList = this.dataList.concat(result);
						// 加载完成后停止加载动画
						this.$refs.guipage.stoploadmore();
						// 假定加载了全部数据，通知组件不再加载更多
						if (this.query.pageNum >= this.totalPage) {
							this.$refs.guipage.nomore();
						}
					}
					// 第一页 有可能是第一次加载或者刷新
					else {
						this.dataList = [];
						this.dataList = result;
						this.noData = result.length ? false : true;
						if (this.query.pageNum >= this.totalPage) {
							this.$refs.guipage.nomore();
						}
						// 刷新
						if (isReload) {
							this.$refs.guipage.endReload();
						}
					}
					this.query.pageNum++;
					this.apiLoadingStatus = false;
				})
			},
			reload: function(isReload = true) {
				this.query.pageNum = 1;
				this.getData(isReload);
			},
		}
	}
</script>

<style>

</style>